package com.cjl.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjl.common.constants.PageConstants;
import com.cjl.common.dtos.PageDto;
import com.cjl.common.dtos.ResponseResult;
import com.cjl.common.enums.HttpEnums;
import com.cjl.food.dtos.FoodDto;
import com.cjl.food.mapper.FoodMapper;
import com.cjl.food.pojos.Food;
import com.cjl.food.service.FoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author cjl
 * @version v1.0
 * @description TODO
 * @createTime 2023/2/14 14:47
 */
@Component
@Slf4j
@Transactional
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    /**
     * 添加菜品
     *
     * @param foodDto
     * @return
     */
    @Override
    public ResponseResult addFood(FoodDto foodDto) {

        //检查参数
        if (checkParameters(foodDto)) return ResponseResult.errorResult(HttpEnums.PARAMS_INVALID);

        //判断菜品是否已存在
        Food dbFood = getOne(new LambdaQueryWrapper<Food>().eq(Food::getName, foodDto.getName()));
        if (dbFood != null) {
            return ResponseResult.errorResult(HttpEnums.FOOD_ALREADY_EXIST, dbFood);
        }

        //拷贝属性
        Food newFood = new Food();
        BeanUtils.copyProperties(foodDto, newFood);
        //保存数据到数据库
        boolean result = save(newFood);
        if (!result) {
            return ResponseResult.errorResult(HttpEnums.DB_UPDATE_DATA_ERROR);
        }
        return ResponseResult.okResult(newFood);
    }

    /**
     * 删除菜品
     *
     * @param foodName
     * @return
     */
    @Override
    public ResponseResult deleteFood(String foodName) {
        //检查参数
        if (StringUtils.isBlank(foodName)) return ResponseResult.errorResult(HttpEnums.PARAMS_INVALID, "请传入菜名");

        //根据菜名获取对象
        Food dbFood = getOne(new LambdaQueryWrapper<Food>().eq(Food::getName, foodName));

        //判断对应菜品是否存在
        if (dbFood == null) return ResponseResult.errorResult(HttpEnums.FOOD_NOT_EXIST);

        //删除菜品
        boolean result = removeById(dbFood.getId());
        if (result) return ResponseResult.okResult("删除成功", dbFood);

        return ResponseResult.errorResult(HttpEnums.DB_UPDATE_DATA_ERROR, "删除失败");
    }

    /**
     * 修改菜品
     *
     * @param foodDto
     * @return
     */
    @Override
    public ResponseResult modifyFood(FoodDto foodDto) {
        //检查参数
        if (checkParameters(foodDto)) return ResponseResult.errorResult(HttpEnums.PARAMS_INVALID);

        //根据菜名获取对象
        Food dbFood = getOne(new LambdaQueryWrapper<Food>().eq(Food::getName, foodDto.getName()));

        //判断是否存在
        if (dbFood == null) return ResponseResult.errorResult(HttpEnums.FOOD_NOT_EXIST);

        //更新属性
        if (StringUtils.isNotBlank(foodDto.getDescription())) dbFood.setDescription(foodDto.getDescription());
        if (foodDto.getPrice() != null) dbFood.setPrice(foodDto.getPrice());
        if (foodDto.getStatus() != null) dbFood.setStatus(foodDto.getStatus());


        //更新菜品
        boolean result = updateById(dbFood);
        if (!result) return ResponseResult.errorResult(HttpEnums.DB_UPDATE_DATA_ERROR, "修改菜品失败");

        return ResponseResult.okResult(dbFood);
    }

    /**
     * 根据菜名搜索菜品
     * 如果菜名不匹配则进行模糊搜索
     *
     * @param foodName
     * @return
     */
    @Override
    public ResponseResult searchFood(String foodName) {
        //检查参数
        if (StringUtils.isBlank(foodName)) return ResponseResult.errorResult(HttpEnums.PARAMS_INVALID, "请传入菜名");

        //根据菜名获取对象
        Food dbFood = getOne(new LambdaQueryWrapper<Food>().eq(Food::getName, foodName));
        //判断是否存在
        if (dbFood == null) {
            List<Food> foods = list(new LambdaQueryWrapper<Food>().like(true, Food::getName, foodName));
            if (foods == null || foods.size() == 0) return ResponseResult.errorResult(HttpEnums.FOOD_NOT_EXIST);
            Map<String,List<Food>> map = new HashMap<>();
            map.put("foods",foods);
            return ResponseResult.okResult(map);
        }

        //存在则直接返回
        return ResponseResult.okResult(dbFood);
    }


    @Autowired
    FoodMapper foodMapper;

    /**
     * 菜品列表
     *
     * @param pageDto 前端分页参数
     * @return ResponseResult
     */
    @Override
    public ResponseResult foodList(PageDto pageDto) {
        Integer current = null;
        Integer size = null;
        if (pageDto != null) {
            size = pageDto.getSize();
            current = pageDto.getCurrent();
        }
        //设置分页参数
        if (size == null || size < PageConstants.MIN_SIZE  || size > PageConstants.MAX_SIZE)
            size = PageConstants.DEFAULT_SIZE;
        if (current == null || current < PageConstants.DEFAULT_CURRENT)
            current = PageConstants.DEFAULT_CURRENT;

        //分页
        IPage<Food> page = new Page<>(current, size);
        foodMapper.selectPage(page, null);
        List<Food> records = page.getRecords();
        Map<String,Object> data = new HashMap<>();
        data.put("records",records);
        return ResponseResult.okResult(data);
    }

    /**
     * 根据id获取菜品
     * @param foodId 菜品id
     * @return ResponseResult
     */
    @Override
    public ResponseResult getFoodById(Integer foodId) {
        Food dbFood = getById(foodId);
        if (dbFood == null) return ResponseResult.errorResult(HttpEnums.FOOD_NOT_EXIST);
        return ResponseResult.okResult(dbFood);
    }

    /**
     * 检查菜品参数
     *
     * @param foodDto
     * @return
     */
    private boolean checkParameters(FoodDto foodDto) {
        return foodDto == null
                || StringUtils.isBlank(foodDto.getName());
    }
}
